package com.abe.libquick.widget.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListPopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.abe.libquick.R;
import com.abe.libquick.utils.NormalUtils;
import com.abe.libquick.utils.quick.EmptyUtils;

import java.util.List;

public class FormSpinnerView extends RelativeLayout {
    private TextView itemLabelName, itemValue;
    private String label, value;
    private ListPopupWindow itemPop;
    private AdapterView.OnItemClickListener listener;
    private List<String> lists;
    private boolean canEdit;
    private Drawable img;

    public FormSpinnerView(Context context) {
        this(context, null);
    }

    public FormSpinnerView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FormSpinnerView(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        //抽取需要的自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.FormSpinnerView);
        int labelRes = typedArray.getResourceId(R.styleable.FormSpinnerView_labelRes, 0);
        String labelStr = typedArray.getString(R.styleable.FormSpinnerView_label);
        int valueRes = typedArray.getResourceId(R.styleable.FormSpinnerView_valueRes, 0);
        String valueStr = typedArray.getString(R.styleable.FormSpinnerView_value);
        if (labelRes != 0) {
            label = getContext().getResources().getString(labelRes);
        }
        if (!TextUtils.isEmpty(labelStr)) {
            label = labelStr;
        }
        if (valueRes != 0) {
            value = getContext().getResources().getString(valueRes);
        }
        if (!TextUtils.isEmpty(valueStr)) {
            value = valueStr;
        }
        canEdit = typedArray.getBoolean(R.styleable.FormSpinnerView_canEdit, true);
        float labelSize = typedArray.getDimension(R.styleable.FormSpinnerView_labelSize, NormalUtils.dip2px(getContext(), 16));
        float valueSize = typedArray.getDimension(R.styleable.FormSpinnerView_valueSize, NormalUtils.dip2px(getContext(), 14));
        float imgSizeW = typedArray.getDimension(R.styleable.FormSpinnerView_imgSizeW, NormalUtils.dip2px(getContext(), 12));
        float imgSizeH = typedArray.getDimension(R.styleable.FormSpinnerView_imgSizeH, NormalUtils.dip2px(getContext(), 12));
        int imgRes = typedArray.getResourceId(R.styleable.FormSpinnerView_img, R.mipmap.icon_down_go);
        typedArray.recycle();
        View view = View.inflate(context, R.layout.item_abe_view_common_form_spinner_text, this);
        itemLabelName = view.findViewById(R.id.item_label_name);
        itemValue = view.findViewById(R.id.item_value);
        itemLabelName.setText(label);
        itemValue.setText(value);
        itemLabelName.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelSize);
        itemValue.setTextSize(TypedValue.COMPLEX_UNIT_PX, valueSize);
        img = context.getResources().getDrawable(imgRes);
        img.setBounds(0, 0, (int) imgSizeW, (int) imgSizeH);
        itemValue.setOnClickListener(v -> {
            if (canEdit) show();
        });
        setCanEdit(canEdit);
    }

    public TextView getItemValue() {
        return itemValue;
    }

    public void setLabel(String label) {
        this.label = label;
        if (itemLabelName != null) {
            itemLabelName.setText(label);
        }
    }

    public void setLabelRes(int labelRes) {
        setLabel(getContext().getResources().getString(labelRes));
    }

    public void setValue(String value) {
        this.value = value;
        if (itemValue != null) {
            itemValue.setText(value);
        }
    }

    public void setValueRes(int valueRes) {
        setValue(getContext().getResources().getString(valueRes));
    }

    public String getText() {
        return itemValue.getText() == null ? "" : itemValue.getText().toString().trim();
    }

    public void setListener(AdapterView.OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setData(List<String> lists) {
        this.lists = lists;
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
        if (itemValue != null) {
            itemValue.setCompoundDrawables(null, null, canEdit ? img : null, null);
        }
    }

    public void show() {
        if (EmptyUtils.isEmpty(lists)) return;
        if (itemPop == null) {
            itemPop = new ListPopupWindow(getContext());
        }
        itemPop.setAdapter(new PopupAdapter(getContext(), lists));
        itemPop.setWidth(itemValue.getWidth());
        itemPop.setModal(true);
        // 设置ListPopupWindow的锚点，即关联PopupWindow的显示位置和这个锚点
        itemPop.setAnchorView(itemValue);
        itemPop.setOnItemClickListener((parent, view, position, id) -> {
            itemPop.dismiss();
            itemValue.setText(lists.get(position));
            if (listener == null) return;
            listener.onItemClick(parent, view, position, id);
        });
        itemPop.show();
    }

    public class PopupAdapter extends BaseAdapter {
        private List<String> list;
        private LayoutInflater mInflater;

        PopupAdapter(Context context, List<String> list) {
            this.list = list;
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_view_normal_popup_right_sim, null);
                holder = new ViewHolder();
                /*得到各个控件的对象*/
                holder.title = convertView.findViewById(R.id.item_name);
                convertView.setTag(holder); //绑定ViewHolder对象
            } else {
                holder = (ViewHolder) convertView.getTag(); //取出ViewHolder对象
            }
            holder.title.setText(list.get(position));
            return convertView;
        }

        /*存放控件 的ViewHolder*/
        final class ViewHolder {
            TextView title;
        }
    }
}
